package cn.gcBlock.service.impl;

import cn.gcBlock.service.BlockCurrencyService;
import cn.gcBlock.utils.PageUtil;
import cn.gcBlock.po.BlockCurrency;
import cn.gcBlock.repository.BlockCurrencyRepository;
import cn.gcBlock.dto.BlockCurrencyDTO;
import cn.gcBlock.dto.BlockCurrencyQueryCriteria;
import cn.gcBlock.service.mapper.BlockCurrencyMapper;
import cn.gcBlock.utils.QueryHelp;
import cn.gcBlock.utils.ValidationUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Pageable;

import java.util.Optional;

/**
* @author jie
* @date 2019-08-22
*/
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class BlockCurrencyServiceImpl implements BlockCurrencyService {

@Autowired
private BlockCurrencyRepository blockCurrencyRepository;

@Autowired
private BlockCurrencyMapper blockCurrencyMapper;

@Override
public Object queryAll(BlockCurrencyQueryCriteria criteria, Pageable pageable){
Page<BlockCurrency> page = blockCurrencyRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
return PageUtil.toPage(page.map(blockCurrencyMapper::toDto));
}

@Override
public Object queryAll(BlockCurrencyQueryCriteria criteria){
return blockCurrencyMapper.toDto(blockCurrencyRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
}

@Override
public BlockCurrencyDTO findById(int id) {
Optional<BlockCurrency> blockCurrency = blockCurrencyRepository.findById(id);
ValidationUtil.isNull(blockCurrency,"未查到数据");
return blockCurrencyMapper.toDto(blockCurrency.get());
}

@Override
@Transactional(rollbackFor = Exception.class)
public BlockCurrencyDTO create(BlockCurrency resources) {
return blockCurrencyMapper.toDto(blockCurrencyRepository.save(resources));
}


@Override
@Transactional(rollbackFor = Exception.class)
public void delete(int id) {
blockCurrencyRepository.deleteById(id);
}
}